#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include <sstream>

using namespace std;
#define WORD_NUM 26

struct TrieNode
{
	int _n;
	TrieNode* arr[WORD_NUM];

	TrieNode():_n(1)
	{
		for (int i = 0; i < WORD_NUM; i++)
		{
			arr[i] = nullptr;
		}
	}
};

class TrieTree
{
	typedef TrieNode Node;
public:
	TrieTree();
	~TrieTree();

	void update_tree(string& s)
	{
		int len = s.length();
		Node* cur = _root;

		for (int i = 0; i < len; i++)
		{
			int idx = s[i] - 'a';
			if (cur->arr[idx] == nullptr)
			{
				cur->arr[idx] = new Node();
			}
			else
			{
				cur->arr[idx]->_n++;
			}
			cur = cur->arr[idx];
		}
	}

	void level_order()
	{
		queue<Node*> q;
		q.push(_root);
		while (!q.empty())
		{
			Node* tmp = q.front();
			q.pop();
			for (int i = 0; i < WORD_NUM; i++)
			{
				if (tmp->arr[i])
				{
					cout << char(i + 'a');
					q.push(tmp->arr[i]);
				}
			}
		}
	}

	void count(string& s)
	{
		int len = s.length();

		Node* cur = _root;
		for (int i = 0; i < len; i++)
		{
			int idx = s[i] - 'a';
			cur = cur->arr[idx];
			if (cur == nullptr)
			{
				cout << 0 << endl;
				return;
			}
		}
		cout << cur->_n << endl;
	}

private:
	Node* _root;
};

TrieTree::TrieTree()
{
	_root = new Node();
}

TrieTree::~TrieTree()
{
}

void test6()
{
	string input;
	while (getline(cin, input))
	{
		vector<string> tokens;
		string token;
		istringstream ss(input);
		while (getline(ss, token, ' '))
		{
			tokens.push_back(token);
		}
		TrieTree tree;

		for (auto& e : tokens)
		{
			tree.update_tree(e);
		}

		tree.level_order();
		cout << endl;
		int t;
		cin >> t;
		while (t--)
		{
			string find;
			cin >> find;
			tree.count(find);
		}

	}
	
}

int main()
{
	test6();
	return 0;
}
